package com.qys.livesMall.navy.task;

import com.qys.livesMall.navy.service.*;
import io.netty.util.HashedWheelTimer;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @menu TaskProducer
 * @date 2023/07/05 17:29
 */
@Slf4j
@Service
public class TaskProducer implements TaskInter{

    private static HashedWheelTimer timer=new HashedWheelTimer(1, TimeUnit.SECONDS, 100);


    @Resource @Lazy
    private NavyShopCollectService navyShopCollectService;

    @Autowired  @Lazy
    private NavyShopOrderServer navyShopOrderServer;

    @Resource @Lazy
    private NavyVideoCommentService navyVideoCommentService;

    @Resource @Lazy
    private NavyLivesGiveLikeService navyLivesGiveLikeService;

    @Resource @Lazy
    private NavyLivesOrderService navyLivesOrderService;

    @Resource @Lazy
    private NavyGoodsCommentService navyGoodsCommentService;

    @Resource @Lazy
    private NavyLivesGiftService navyLivesGiftService;

    @Resource @Lazy
    private NavyLivesBulletScreenService navyLivesBulletScreenService;

    @Resource @Lazy
    private NavyShopAutoOrderServer navyShopAutoOrderServer;

    /**
     * 订单超时处理
     * @param times
     * @param orderId
     */
    public void addOrderOutTimePayTask(int times,Long orderId){
        timer.newTimeout(timeout -> {

            System.out.println(Thread.currentThread().getName());
            System.out.println("执行一个任务，ID = 1, time= " + LocalDateTime.now());
            System.out.println("订单id： " + orderId);
        }, times, TimeUnit.SECONDS);
    }

    /**
     * 批量收藏店铺
     * @param times
     * @param taskDetailId
     */
    public void addShopCollectTask(int times, Long taskDetailId){
        timer.newTimeout(timeout -> {
            navyShopCollectService.exc(taskDetailId);
        }, times, TimeUnit.SECONDS);
    }
    /**
     * 批量短视频评论
     * @param times
     * @param taskDetailId
     */
    public void addVideoCommentTask(int times, Long taskDetailId){
        timer.newTimeout(timeout -> {
            navyVideoCommentService.exc(taskDetailId);
        }, times, TimeUnit.SECONDS);
    }
    /**
     * 批量收藏主播
     * @param times
     * @param taskDetailId
     */
    public void addLivesGiveLikeTask(int times, Long taskDetailId){
        timer.newTimeout(timeout -> {
            navyLivesGiveLikeService.exc(taskDetailId);
        }, times, TimeUnit.SECONDS);
    }

    /**
     * 批量直播下单
     * @param times
     */
    @Override
    public void addLivesOrderTask(int times, Long taskId) {
        timer.newTimeout(timeout -> {
            navyLivesOrderService.exc(taskId);
        }, times, TimeUnit.SECONDS);
    }

    /**
     * 批量商品评论
     * @param times
     * @param taskDetailId
     */
    @Override
    public void addGoodsCommentTask(int times, Long taskDetailId) {
        timer.newTimeout(timeout -> {
            navyGoodsCommentService.exc(taskDetailId);
        }, times, TimeUnit.SECONDS);
    }

    /**
     * 批量直播礼物
     * @param times
     * @param taskDetailId
     */
    @Override
    public void addLivesGiftTask(int times, Long taskDetailId) {
        timer.newTimeout(timeout -> {
            navyLivesGiftService.exc(taskDetailId);
        }, times, TimeUnit.SECONDS);
    }

    /**
     * 批量直播弹幕
     * @param times
     * @param taskDetailId
     */
    @Override
    public void addLivesBulletScreenTask(int times, Long taskDetailId) {
        timer.newTimeout(timeout -> {
            navyLivesBulletScreenService.exc(taskDetailId);
        }, times, TimeUnit.SECONDS);
    }

    @Override
    public void addAutoOrderTask(int times, Long shopId) {
        timer.newTimeout(timeout -> {
            navyShopAutoOrderServer.exc(shopId);
        }, times, TimeUnit.SECONDS);
    }

    /**
     * 水军打单
     * @param times
     */
    @Override
    public void addShopOrderTask(int times, Long taskDetailId) {
        timer.newTimeout(timeout -> {
            navyShopOrderServer.exc(taskDetailId);
        }, times, TimeUnit.SECONDS);
    }

    /**
     * 批量收藏主播
     * @param times
     * @param anchors
     * @param navyId
     */
    public void addAnchorCollectTask(int times, List<Long> anchors, Long navyId){
        timer.newTimeout(timeout -> {

            System.out.println(Thread.currentThread().getName());
            System.out.println("执行一个任务，ID = 1, time= " + LocalDateTime.now());
            System.out.println("任务Id： " + anchors+navyId);
        }, times, TimeUnit.SECONDS);
    }

    /**
     *
     * @param times
     * @param taskId
     */
    public void addTask(int times,Long taskId){
        timer.newTimeout(timeout -> {

            System.out.println(Thread.currentThread().getName());
            System.out.println("执行一个任务，ID = 1, time= " + LocalDateTime.now());
            System.out.println("任务Id： " + taskId);
        }, times, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        TaskProducer producer=new TaskProducer();
        producer.addOrderOutTimePayTask(20,1212L);
    }

}

